import { reactive, onMounted, defineComponent, toRefs, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import inventoryApi from '@/api/inwarehouse/inventory-api'
import warehouseApi from '@/api/inwarehouse/warehouse-api'
import ownerApi from '@/api/inwarehouse/owner-api'
import ZlrtTable from '@/components/zlrt-table/table.vue'
import ZlrtSearch from '@/components/zlrt-search/search.vue'
import TableContext from '@/components/zlrt-table/table-context'

export default defineComponent({
  components: {
    ZlrtTable,
    ZlrtSearch
  },
  setup() {

    const ctx = reactive(new TableContext(getCurrentInstance(), {
      title: '库存',
      api: inventoryApi,
      columns: [
        { prop: 'projectName', label: '项目名称', width: 300 },
        { prop: 'skuName', label: '物料名称', valueType: 'slot', width: 600 },
        { prop: 'barCode', label: '物料编码', valueType: 'input', width: 120 },
        { prop: 'number', label: '库存', width: 100 },
        { prop: 'serialNumber', label: '出厂编号', width: 120 },
        { prop: 'specification', label: '规格型号', width: 200 },
        { prop: 'unit', label: '单位', width: 90 },
        { prop: 'areaName', label: '库区', width: 150 },
        { prop: 'locationName', label: '库位名称', valueType: 'input', width: 200 },
        { prop: 'locationCode', label: '库位编码', valueType: 'input', width: 120 },
      ],
      rules: {
        barCode: [{ required: true, message: '请输入物料编码', trigger: 'blur' }],
        skuName: [{ required: true, message: '请输入品名', trigger: 'blur' }]
      },
      other: {
        handleWarehouse(val) {
          const obj = data.warehouseOptions.find(item => {
            return item.id === val
          })
          ctx.editData.warehouseName = obj['name']
        },
        handleOwner(val) {
          const obj = data.ownerOptions.find(item => {
            return item.id === val
          })
          ctx.editData.ownerName = obj['name']
        }
      }
    }))

    const data = reactive({
      ctx,
      warehouseOptions: [],
      ownerOptions: []
    })

    const getWarehouseList = async () => {
      const params = { filter: { disabled: false } }
      const res = await warehouseApi.list(params)
      data.warehouseOptions = res.data.records
    }

    const getOwnerList = async () => {
      const params = { filter: { disabled: false } }
      const res = await ownerApi.list(params)
      data.ownerOptions = res.data.records

    }

    // init
    onMounted(() => {
      // getWarehouseList()
      // getOwnerList()
      ctx.loadData()
    })

    return {
      ...toRefs(data)
    }
  }
})
